package com.sduept.nwld.mobile.rest;


import com.alibaba.fastjson.JSONArray;
import com.sduept.cim.ledger.total.model.ProtectionDeviceCount;
import com.sduept.nwld.dataserver.manager.cim.ProtectionDeviceCountQueryManager;
import com.sduept.nwld.mobile.manager.MobileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/protectAccount")
public class ProtectAccountService {
	@Autowired
	private ProtectionDeviceCountQueryManager pdcq;
	//SQL注入检测类
	@Inject
	private MobileUtil mobileUtil;
	
	/**
	 *按照调管单位获取保护设备台账
	 */

	@GetMapping("/getProtectAccount/{year}/{voltage}")
	public String getProtectAccount(@PathVariable("year") String year,@PathVariable("voltage") String voltage){
		try {
			year = mobileUtil.SQLInjectCheck(year);
			voltage =mobileUtil.SQLInjectCheck(voltage);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		year = year.replace("年", "");
		voltage = voltage.replace("kV", "").replace("KV", "").replace("kv", "");
		List<ProtectionDeviceCount> counts=new ArrayList<ProtectionDeviceCount>();
		if("全网".equals(voltage)) {
			counts=pdcq.getDispatchProtectionDeviceCount(Integer.parseInt(year));
		}else {
			counts=pdcq.getDispatchProtectionDeviceCountByYearTypeVoltage(Integer.parseInt(year),Integer.parseInt(voltage));
		}
		List<ProtectionDeviceCountDTO> dtos=getProtectionDeviceCountDTOs(counts);
/*		if("全网".equals(voltage)) {
			voltage="all";
			dtos=pecm.genProtectionEquipmentCountDTOByUnit4AllVols(voltage,year);
		}else {
			dtos=pecm.genProtectionEquipmentCountDTOByUnit(voltage,year);
		}
		if(dtos.size()>0) {
			dtos.add(pecm.getTotalCountDTO(dtos));
		}
*/		return JSONArray.toJSONString(dtos);
	}
	private List<ProtectionDeviceCountDTO> getProtectionDeviceCountDTOs(List<ProtectionDeviceCount> counts){
		Map<String,ProtectionDeviceCountDTO> map=new HashMap<>();
		for (ProtectionDeviceCount deviceCount : counts) {
			String unitName=deviceCount.getUnitName();
			ProtectionDeviceCountDTO dto=map.get(unitName);
			if(null==dto) {
				dto=new ProtectionDeviceCountDTO();
				dto.setUnit(unitName);
				dto.setVoltage(deviceCount.getVoltageLevel());
				map.put(unitName, dto);
			}
			setCountNumByStatisticType(dto,deviceCount);
		}
		List<ProtectionDeviceCountDTO> dtos=new ArrayList<>(map.values());
		//计算每个单位的合计
		for (ProtectionDeviceCountDTO protectionDeviceCountDTO : dtos) {
			protectionDeviceCountDTO.setSummary(getSummary(protectionDeviceCountDTO));
		}
		//汇总
		if(!counts.isEmpty()) {
			dtos.add(getSummaryDto(dtos));
		}
		return dtos;
	}
	private ProtectionDeviceCountDTO getSummaryDto(List<ProtectionDeviceCountDTO> dtos) {
		ProtectionDeviceCountDTO statisticDeviceDto=new ProtectionDeviceCountDTO();
		statisticDeviceDto.setUnit("合计");
		for (ProtectionDeviceCountDTO dto : dtos) {
			statisticDeviceDto.setAlternatorProCount(statisticDeviceDto.getAlternatorProCount()+dto.getAlternatorProCount());
			statisticDeviceDto.setBreakerProCount(statisticDeviceDto.getBreakerProCount()+dto.getBreakerProCount());
			statisticDeviceDto.setBusProCount(statisticDeviceDto.getBusProCount()+dto.getBusProCount());
			statisticDeviceDto.setLineProCount(statisticDeviceDto.getLineProCount()+dto.getLineProCount());
			statisticDeviceDto.setNetworkProCount(statisticDeviceDto.getNetworkProCount()+dto.getNetworkProCount());
			statisticDeviceDto.setReactorProCount(statisticDeviceDto.getReactorProCount()+dto.getReactorProCount());
			statisticDeviceDto.setRecorderProCount(statisticDeviceDto.getRecorderProCount()+dto.getRecorderProCount());
			statisticDeviceDto.setSeriesCompensationProCount(statisticDeviceDto.getSeriesCompensationProCount()+dto.getSeriesCompensationProCount());
			statisticDeviceDto.setShortLeadProCount(statisticDeviceDto.getShortLeadProCount()+dto.getShortLeadProCount());
			statisticDeviceDto.setTransformerProCount(statisticDeviceDto.getTransformerProCount()+dto.getTransformerProCount());
			statisticDeviceDto.setUnknowProCount(statisticDeviceDto.getUnknowProCount()+dto.getUnknowProCount());
			statisticDeviceDto.setSummary(statisticDeviceDto.getSummary()+dto.getSummary());
		}
		return statisticDeviceDto;
	}
	private Long getSummary(ProtectionDeviceCountDTO dto) {
		Long c=0l;
		c+=dto.getAlternatorProCount();
		c+=dto.getBreakerProCount();
		c+=dto.getBusProCount();
		c+=dto.getLineProCount();
		//c+=dto.getNetworkProCount();
		c+=dto.getReactorProCount();
		c+=dto.getRecorderProCount();
		c+=dto.getSeriesCompensationProCount();
		c+=dto.getShortLeadProCount();
		c+=dto.getTransformerProCount();
		//c+=dto.getUnknowProCount();
		return c;
	}
	private ProtectionDeviceCountDTO setCountNumByStatisticType(ProtectionDeviceCountDTO dto,
			ProtectionDeviceCount count) {
		int statisticType = count.getStatisticType();
		Long countNum = count.getStatisticCount() == 0 ? null : count.getStatisticCount();// 前台修改为编辑时可以存0，所以展示时0的和null的保持一致。
		switch (statisticType) {
		case -1:
			dto.setUnknowProCount(countNum);
			break;
		case 0:
			dto.setLineProCount(countNum);
			break;
		case 1:
			dto.setBusProCount(countNum);
			break;
		case 2:
			dto.setTransformerProCount(countNum);
			break;
		case 3:
			dto.setAlternatorProCount(countNum);
			break;
		case 4:
			dto.setReactorProCount(countNum);
			break;
		case 5:
			dto.setSeriesCompensationProCount(countNum);
			break;
		case 6:
			dto.setShortLeadProCount(countNum);
			break;
		case 7:
			dto.setBreakerProCount(countNum);
			break;
		case 8:
			dto.setRecorderProCount(countNum);
			break;
		case 9:
			dto.setNetworkProCount(countNum);
			break;
		}
		return dto;
	}
	public class ProtectionDeviceCountDTO {

		/**
		 * 统计维度名称
		 */
		private String unit;

		private int voltage;
		/**
		 * 保护类型 -1：未知 0：线路、辅助保护 1：母线保护 2：变压器保护 3：发电机保护 4：电抗器保护 5：串补保护 6：短引线保护 7：断路器保护
		 * 8：故障录波器 9：网络设备
		 */
		private long unknowProCount;// 未知类型
		private long lineProCount;// 线路保护
		private long busProCount;// 母线保护
		private long transformerProCount;// 变压器
		private long alternatorProCount;// 发电机
		private long reactorProCount;// 电抗器
		private long seriesCompensationProCount;// 串补保护
		private long shortLeadProCount;// 短引线保护
		private long breakerProCount;// 断路器保护
		private long recorderProCount;// 录波器保护
		private long networkProCount;// 网络设备
		private long summary;// 汇总

		public int getVoltage() {
			return voltage;
		}

		public void setVoltage(int voltage) {
			this.voltage = voltage;
		}

		public long getUnknowProCount() {
			return unknowProCount;
		}

		public void setUnknowProCount(long unknowProCount) {
			this.unknowProCount = unknowProCount;
		}

		public long getLineProCount() {
			return lineProCount;
		}

		public void setLineProCount(long lineProCount) {
			this.lineProCount = lineProCount;
		}

		public long getBusProCount() {
			return busProCount;
		}

		public void setBusProCount(long busProCount) {
			this.busProCount = busProCount;
		}

		public long getTransformerProCount() {
			return transformerProCount;
		}

		public void setTransformerProCount(long transformerProCount) {
			this.transformerProCount = transformerProCount;
		}

		public long getAlternatorProCount() {
			return alternatorProCount;
		}

		public void setAlternatorProCount(long alternatorProCount) {
			this.alternatorProCount = alternatorProCount;
		}

		public long getReactorProCount() {
			return reactorProCount;
		}

		public void setReactorProCount(long reactorProCount) {
			this.reactorProCount = reactorProCount;
		}

		public long getSeriesCompensationProCount() {
			return seriesCompensationProCount;
		}

		public void setSeriesCompensationProCount(long seriesCompensationProCount) {
			this.seriesCompensationProCount = seriesCompensationProCount;
		}

		public long getShortLeadProCount() {
			return shortLeadProCount;
		}

		public void setShortLeadProCount(long shortLeadProCount) {
			this.shortLeadProCount = shortLeadProCount;
		}

		public long getBreakerProCount() {
			return breakerProCount;
		}

		public void setBreakerProCount(long breakerProCount) {
			this.breakerProCount = breakerProCount;
		}

		public long getRecorderProCount() {
			return recorderProCount;
		}

		public void setRecorderProCount(long recorderProCount) {
			this.recorderProCount = recorderProCount;
		}

		public long getNetworkProCount() {
			return networkProCount;
		}

		public void setNetworkProCount(long networkProCount) {
			this.networkProCount = networkProCount;
		}

		public long getSummary() {
			return summary;
		}

		public void setSummary(long summary) {
			this.summary = summary;
		}

		public String getUnit() {
			return unit;
		}

		public void setUnit(String unit) {
			this.unit = unit;
		}


	}

}
